package com.opNaga.bpmp.connectingObject.sequenceFlow;

import com.opNaga.bpmp.BPMPElement;
import com.opNaga.bpmp.connectingObject.ConnectingObject;

/**
 * User: Junyi BEI
 * Date: 19/01/2012
 * Time: 14:00
 */
public class SequenceFlow extends ConnectingObject {
  public SequenceFlow() {
    super();
  }

  public SequenceFlow(String t) {
    super(t);
  }

  @Override
  public String getType() {
    return "Sequence Flow";
  }

  @Override
  public boolean destroy() {
    if(source != null && !((SequenceFlowSource) source).removeOutwardSequenceFlow(this)) {
      return standardMessage(810, source);
    }
    if(target != null && !((SequenceFlowTarget) target).removeInwardSequenceFlow(this)) {
      return standardMessage(811, target);
    }
    return super.destroy();
  }

  @Override
  public boolean validateTarget(BPMPElement targetElement) {
    if(!SequenceFlowTarget.class.isAssignableFrom(targetElement.getClass())) {
      return standardMessage(350, targetElement);
    }
    BPMPElement root = ((SequenceFlowTarget) targetElement).getRoot();
    if(root == null) {
      return standardMessage(351, targetElement);
    }
    if(source != null && ((SequenceFlowTarget) targetElement).getRoot() != ((SequenceFlowSource) source).getRoot()) {
      return standardMessage(352, targetElement, source);
    }
    if(!((SequenceFlowTarget) targetElement).addInwardSequenceFlow(this)) {
      return standardMessage(354, targetElement);
    }
    return standardMessage(392, targetElement);
  }

  @Override
  public boolean validateSource(BPMPElement sourceElement) {
    if(!SequenceFlowSource.class.isAssignableFrom(sourceElement.getClass())) {
      return standardMessage(360, sourceElement);
    }
    BPMPElement root = ((SequenceFlowSource) sourceElement).getRoot();
    if(root == null) {
      return standardMessage(361, sourceElement);
    }
    if(target != null && root != ((SequenceFlowTarget) target).getRoot()) {
      return standardMessage(362, sourceElement, target);
    }
    if(!((SequenceFlowSource) sourceElement).addOutwardSequenceFlow(this)) {
      return standardMessage(364, sourceElement);
    }
    return standardMessage(393, sourceElement);
  }
}
